import { createWebHistory, createRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import { ElMessage } from 'element-plus'

import Login from '../view/Login/Login.vue'
import client from '../view/Client/Client.vue'
import HomePage from '../view/Client/HomePage.vue'
import MyCart from '../view/Client/MyCart.vue'
import ProductDetail from '../view/Client/ProductDetail.vue'
import Category from '../view/Client/Category.vue'
import OrderConfirm from '../view/Client/OrderConfirm.vue'
import MyOrders from '../view/Client/MyOrders.vue'

// 管理后台页面
import Admin from '../view/Admin/Admin.vue'
import Dashboard from '../view/Admin/Dashboard.vue'
import GoodsList from '../view/Admin/GoodsList.vue'
import CategoryList from '../view/Admin/CategoryList.vue'
import OrderList from '../view/Admin/OrderList.vue'
import UserList from '../view/Admin/UserList.vue'
import CommentsList from '../view/Admin/CommentsList.vue'
import StandardList from '../view/Admin/StandardList.vue'
import IconList from '../view/Admin/IconList.vue'

const routes = [
  { 
    path: '/', 
    component: Login
  },
  { 
    path: '/client',
    component:client,
    children: [
    {
      path:'HomePage',
      component:HomePage
    },
    {
      path:'MyCart',
      component:MyCart,
      meta: {
        requireAuth: true
      }
    },
    {
      path:'OrderConfirm',
      component: OrderConfirm,
      meta: {
        requireAuth: true
      }
    },
    {
      path:'MyOrders',
      component: MyOrders,
      meta: {
        requireAuth: true
      }
    },
    {
      path:'product/:id',
      component: ProductDetail
    },
    {
      path:'category/:category?',
      component: Category
    },
    
  ]
  },
  // 管理后台路由
  {
    path: '/admin',
    name: 'Admin',
    component: Admin,
    meta: {
      requireAuth: true,
      admin: true
    },
    children: [
      {
        path: '',
        name: 'Dashboard',
        component: Dashboard,
        meta: {
          requireAuth: true,
          admin: true
        }
      },
      {
        path: 'goods',
        name: 'GoodsList',
        component: GoodsList,
        meta: {
          requireAuth: true,
          admin: true
        }
      },
      {
        path: 'category',
        name: 'CategoryList',
        component: CategoryList,
        meta: {
          requireAuth: true,
          admin: true
        }
      },
      {
        path: 'order',
        name: 'OrderList',
        component: OrderList,
        meta: {
          requireAuth: true,
          admin: true
        }
      },
      {
        path: 'user',
        name: 'UserList',
        component: UserList,
        meta: {
          requireAuth: true,
          admin: true
        }
      },
      {
        path: 'comment',
        name: 'CommentsList',
        component: CommentsList,
        meta: {
          requireAuth: true,
          admin: true
        }
      },
      {
        path: 'standard',
        name: 'StandardList',
        component: StandardList,
        meta: {
          requireAuth: true,
          admin: true
        }
      },
      {        path: 'icon',        name: 'IconList',        component: IconList,        meta: {          requireAuth: true,          admin: true        }      },      {        path: 'files',        name: 'FileManager',        component: () => import('../view/Admin/FileManager.vue'),        meta: {          requireAuth: true,          admin: true        }      },      {        path: 'slides',        name: 'SlideList',        component: () => import('../view/Admin/SlideList.vue'),        meta: {          requireAuth: true,          admin: true        }      }    ]  },  {
    path: '/:pathMatch(.*)*',
     redirect: '/' 
  }
]

const router = createRouter({
  // 使用createWebHistory替代createMemoryHistory，这样路由可以正确反映URL变化
  history: createWebHistory(),
  routes,
})

// 路由守卫：购物车需要登录
router.beforeEach((to, _from, next) => {
  // 临时跳过登录验证，以便直接访问管理后台进行测试
  // 实际项目中应该恢复下面的代码
  
  // const auth = useAuthStore()
  // 检查是否需要登录
  // if (to.meta.requireAuth) {
  //   if (!auth.isLoggedIn) {
  //     ElMessage.warning('请先登录')
  //     return next('/')
  //   } else {
  //     // 检查是否需要管理员权限
  //     if (to.meta.admin) {
  //       // 这里简化处理，为了方便测试，我们直接允许访问
  //       // 实际项目中应该从用户信息中获取权限
  //       next()
  //     } else {
  //       next()
  //     }
  //   }
  // } else {
  //   next()
  // }
  
  // 直接允许所有路由访问
  next()
})

export default router